﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.LengthOfLongestSubstring
{
    class MainProject
    {
        static void Main(string[] args)
        {
            Console.WriteLine(LengthOfLongestSubstring1("dvdf"));
            Console.ReadKey();
        }


      
        /// <summary>
        /// 暴力解法(KMP肯定也可以)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int LengthOfLongestSubstring2(string s)
        {
            int maxLeng = 0;
            HashSet<char> hash = new HashSet<char>();
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = i; j < s.Length; j++)
                {
                    if (!hash.Add(s[j]))
                    {
                        maxLeng = hash.Count > maxLeng ? hash.Count : maxLeng;
                        hash.Clear();
                        break;
                    }
                }
            }
            maxLeng = hash.Count > maxLeng ? hash.Count : maxLeng;
            return maxLeng;
        }

        /// <summary>
        /// dvdf(有问题)----滑动窗口
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int LengthOfLongestSubstring1(string s)
        {
            Queue<int> que = new Queue<int>();
            int maxLeng = 0;
            foreach (var item in s)
            {
                if (que.Contains(item))
                {
                    maxLeng = que.Count > maxLeng ? que.Count : maxLeng;
                    while (que.Peek() != item)
                    {
                        que.Dequeue();
                    }
                    que.Dequeue();
                    que.Enqueue(item);
                }
                else
                {
                    que.Enqueue(item);
                }
            }
            maxLeng = que.Count > maxLeng ? que.Count : maxLeng;
            return maxLeng;
        }
    }
}
